<?

function valid_mail($email) {

	if(!eregi("[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}", $email)) {
		return "";
	} else {
		return $email;
	}
}


function mail_convert($str_email, $str_text) {

	if (preg_match("/@/", $str_text)) {
		$str_text = '<script type="text/javascript">/*<![CDATA[*/'
		. 'document.write(\''
		. wordwrap($str_text, rand(2,4), "'+'", 1)
		. '\')'
		. '/*]]>*/</script>';
	}
	return '<a href="#" onmouseover="this.href=\''
	. wordwrap('mailto:' . $str_email, rand(2,4), "'+'", 1)
	. '\'">'
	. $str_text
	. '</a>';
}

/**
* @return void
* @param string $arg_name
* @desc Manager of guestbook
**/

function get_gb_records($guest_path='guestbook', $posts_per_page=20, $arg_name="guestbook.xml"){
	global $sapi_obj, $env, $http_path, $MODREWRITE;

    $base_url=$http_path .(($MODREWRITE=="disabled")?'?vpath=':'').  $guest_path.'/';
    
    $stream=array();
	$filename=$GLOBALS["root_path"] . "usr/xml/vdb/".$arg_name;

	$vdb = new sapi_vdb($filename);

	if (strlen($_POST["gb"]["message"])>0 and strlen($_POST["gb"]["name"])>0) {
		$data["row"]=$_POST["gb"];
		$_POST["gb"]["message"]=stripslashes($_POST["gb"]["message"]);
		preg_match_all("/\[code\](.*)\[\/code\]/isU", $_POST["gb"]["message"], $matches);
		if (is_array($matches[0])) {
			foreach ($matches[0] as $k=>$code) {
				$codearr[$k]=$code;
				$_POST["gb"]["message"]=preg_replace("/\[code\](.*)\[\/code\]/isU", "[codearr_".$k."]", $_POST["gb"]["message"], 1);
			}

			$_POST["gb"]["message"]=htmlspecialchars($_POST["gb"]["message"]);
			if(is_array($codearr))
			foreach ($codearr as $k=>$code) {
				$_POST["gb"]["message"]=preg_replace("/\[codearr_".$k."\]/is", $code, $_POST["gb"]["message"], 1);
			}
		}
		$data["row"]["message"]=$_POST["gb"]["message"];
		$data["row"]["email"]=valid_mail($_POST["gb"]["email"]);
		$data["row"]["name"]=htmlspecialchars($_POST["gb"]["name"]);
		$data["post_action"]="ADDROW";
		$vdb->save_data_row(false, $data, false);
		header("Location: " . $base_url);
		exit;
	}

	if ($env["area51"] and $_POST["delete_id"]) {
		$vdb->delete_data_row($_POST["delete_id"]);
	}
	$stream=array();
	$vdb->objdata=array();
	$stream=$vdb->get_data($filename);

	if(isset($env["virtual_path_mask"])) $page=$env["page"]["VAR"]; else $page=1;
	$pages=ceil(count($stream)/$posts_per_page);
	$start_limit=($page-1)*$posts_per_page + 1;
	$finish_limit=$page*$posts_per_page +1 ;
	if($pages>1){
		$sapi_obj->env["navigation"]="<div align=\"center\" class=\"quote\"><b>????????:</b>&nbsp;";
		for ($i=1; $i<=$pages; $i++){
			$sapi_obj->env["navigation"].=($i==$page?"&nbsp;<b>" . $i . "</b>&nbsp;":'&nbsp;<a href="'.$base_url.$i.'/">'.$i.'</a>&nbsp;');
		}
		$sapi_obj->env["navigation"].="</div>";
	}
	$cnt=0;
	if (is_array($stream))
	foreach ($stream as $key=>$value) {
		if($key<$start_limit or $key>=$finish_limit) unset($stream[$key]); else
		$tstream[++$cnt]=$value;
	}
	$stream=$tstream;
	if (is_array($stream))
	foreach ($stream as $key=>$value) {
		$stream[$key]["COUNTER"]=$key;
		$stream[$key]["AUTHOR"]=mail_convert($stream[$key]["EMAIL"],$stream[$key]["NAME"]);
		$stream[$key]["MESSAGE"]=nl2br($stream[$key]["MESSAGE"]);

		preg_match_all("/\[code\](.*)\[\/code\]/isU", $value["MESSAGE"], $matches);
		if (is_array($matches[1])) {
			foreach ($matches[1] as $k=>$code) {
				$php[$k]="<div class=\"code\">".highlight_string(wordwrap($code), true) . "</div>";
			}
			$i=0;
			if (is_array($php))
			foreach ($php as $code) {
				$stream[$key]["MESSAGE"]=preg_replace("/\[code\](.*)\[\/code\]/isU", $code, $stream[$key]["MESSAGE"], 1);
			}
		}
	}
	$smiles=array(
	":hello:"=>'<img src="'.$http_path.'usr/templates/images/smiles/hello.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":sorry:"=>'<img src="'.$http_path.'usr/templates/images/smiles/sorry.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":think:"=>'<img src="'.$http_path.'usr/templates/images/smiles/think.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":up:"=>'<img src="'.$http_path.'usr/templates/images/smiles/up.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":appl:"=>'<img src="'.$http_path.'usr/templates/images/smiles/appl.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":bad:"=>'<img src="'.$http_path.'usr/templates/images/smiles/bad.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":bye:"=>'<img src="'.$http_path.'usr/templates/images/smiles/bye.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	":cool:"=>'<img src="'.$http_path.'usr/templates/images/smiles/cool.gif" border="0" alt="" hspace="5" vspace="3"  title="" />',
	);

	foreach ($smiles as $key=>$value){
		$pattern[]="/ " . $key . " /";
		$replacement[]=$value;
	}
	ksort($pattern);
	ksort($replacement);

	if(is_array($stream))
	foreach ($stream as $key=>$entry) {
		$stream[$key]["MESSAGE"]=preg_replace($pattern, $replacement, $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[b\](.*)\[\/b\]/isU", "<b>\\1</b>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[i\](.*)\[\/i\]/isU", "<i>\\1</i>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[u\](.*)\[\/u\]/isU", "<span style=\"text-decoration: underline;\">\\1</span>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[color=(\w+)\](.*)\[\/color\]/isU", "<span style=\"color: \\1\">\\2</span>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[size=(\d{1,2})\](.*)\[\/size\]/isU", "<span style=\"font-size: \\1px;\">\\2</span>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[quote=(\w+)\](.*)\[\/quote\]/isU", "<div class=\"quote\"><div class=\"header\"><b>\\1</b> writes:</div>\\2</div>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[url=(.*)\](.*)\[\/url\]/isU", "<a href=\"\\1\">\\2</a>", $stream[$key]["MESSAGE"]);
		$stream[$key]["MESSAGE"]=preg_replace("/\[quote](.*)\[\/quote\]/isU", "<div class=\"quote\"><div class=\"header\"><b>Quote:</b></div>\\1</div>", $stream[$key]["MESSAGE"]);
	}

	return $stream;
}
?>